// 显示登录框并隐藏欢迎区域
document.getElementById('showLoginBtn').addEventListener('click', function() {
    document.getElementById('brandIntro').style.display = 'none';
    document.getElementById('loginCard').style.display = 'block';
    document.getElementById('loginCard').style.alignSelf = 'center';
});

// 添加用户名输入框监听事件
document.getElementById('username').addEventListener('input', function() {
    const username = this.value;
    if (username) {
        // 调用后端API获取密码
        fetch(`/api/get_password?username=${encodeURIComponent(username)}`)
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 自动填充密码框
                    document.getElementById('password').value = data.password;
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
    }
});

// 控制管理员密钥输入框的显示
const roleSelect = document.getElementById('role');
const adminKeyGroup = document.getElementById('adminKeyGroup');

if (roleSelect) {
    roleSelect.addEventListener('change', function() {
        if (this.value === '管理员') {
            adminKeyGroup.style.display = 'block';
        } else {
            adminKeyGroup.style.display = 'none';
        }
    });
}

// 等待页面加载完成
window.addEventListener('load', function() {
    // 尝试从本地存储加载用户名和密码
    const savedUsername = localStorage.getItem('savedUsername');
    if (savedUsername) {
        document.getElementById('username').value = savedUsername;
        document.getElementById('rememberPassword').checked = true;

        // 自动填充密码
        fetch(`/api/get_password?username=${encodeURIComponent(savedUsername)}`)
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    document.getElementById('password').value = data.password;
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
    }
});

// 表单提交时处理记住密码逻辑
const loginForm = document.querySelector('form[method="POST"]');
if (loginForm) {
    loginForm.addEventListener('submit', function(event) {
        if (document.getElementById('rememberPassword').checked) {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            // 将用户名存储到本地存储
            localStorage.setItem('savedUsername', username);

            // 调用后端API存储密码
            fetch('/api/set_password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
            })
            .then(response => response.json())
            .then(data => {
                if (!data.success) {
                    console.error('Failed to save password:', data.message);
                }
            })
            .catch(error => {
                console.error('Error saving password:', error);
            });
        } else {
            // 如果不记住密码，清除本地存储
            localStorage.removeItem('savedUsername');
        }
    });
}